package io.wins.vertx.verticle.api;

import io.vertx.core.Future;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.wins.vertx.database.JdbcExecutor;
import io.wins.vertx.entity.Goods;
import io.wins.vertx.service.GoodsService;

public class GoodsApiVerticle extends RestApiVerticle {

    public static final String REST_ADDRESS = "io.wins.rest.goods#%1$s";
    public static final String CREATE = "createGoods";
    public static final String UPDATE = "updateGoods";
    public static final String DELETE = "deleteGoods";
    public static final String LIST = "listGoods";
    public static final String LOAD = "loadGoods";
    public static final String PUBLISH = "publishGoods";
    private static final Logger logger = LoggerFactory.getLogger(GoodsApiVerticle.class);

    public final GoodsService service;

    public GoodsApiVerticle(final GoodsService service) {
        this.service = service;
    }

    @Override
    public void start(final Future<Void> startFuture) throws Exception {
        super.start();
        this.on(String.format(REST_ADDRESS, CREATE), this::createGoods);
        this.on(String.format(REST_ADDRESS, UPDATE), this::updateGoods);
        this.on(String.format(REST_ADDRESS, DELETE), this::deleteGoods);
        this.on(String.format(REST_ADDRESS, LIST), this::listGoods);
        this.on(String.format(REST_ADDRESS, LOAD), this::loadGoods);
        this.on(String.format(REST_ADDRESS, PUBLISH), this::publish);
        startFuture.complete();
    }

    private void updateGoods(final Message<JsonObject> tMessage) {
        final JsonObject body = tMessage.body().getJsonObject("body").getJsonObject("goods");
        final Goods goods = new Goods(body);
        this.service.updateGoods(goods, tMessage);
    }

    private void loadGoods(final Message<JsonObject> tMessage) {
        final JsonObject param = tMessage.body().getJsonObject("param");
        final String id = param.getString("id");
        this.service.getGoodsById(id, tMessage);
    }

    private void listGoods(final Message<JsonObject> tMessage) {
        final JsonObject param = tMessage.body().getJsonObject("param");
        final String init = param.getString("init");
        final int page = Integer.valueOf(param.getString("page"));
        final int limit = Integer.valueOf(param.getString("limit"));
        if ("1".equals(init)) {
            this.vertx.eventBus().send(JdbcExecutor.CONSUMER, new JsonObject().put("action", "select")
                .put("sql", "select count(*) as total from ds_goods"), r -> {
                if (r.succeeded()) {
                    tMessage.body().put("total", ((JsonArray) r.result().body()).getJsonObject(0).getValue("total"));
                    this.service.listGoodsByPage(page, limit, tMessage);
                } else {
                    this.service.listAllGoods(tMessage);
                }
            });
        } else {
            this.service.listGoodsByPage(page, limit, tMessage);
        }
    }

    private void deleteGoods(final Message<JsonObject> tMessage) {
        final JsonObject param = tMessage.body().getJsonObject("param");
        final String id = param.getString("id");
        this.service.deleteGoods(id, tMessage);
    }

    private void createGoods(final Message<JsonObject> tMessage) {
        final JsonObject body = tMessage.body().getJsonObject("body").getJsonObject("goods");
        final Goods goods = new Goods(body);
        this.service.insertGoods(goods, tMessage);
    }

    private void publish(final Message<JsonObject> tMessage) {
        final String id = tMessage.body().getJsonObject("param").getString("id");
        final String onSell = tMessage.body().getJsonObject("body").getString("onSell");
        this.service.publish(id, onSell, tMessage);
    }
}
